<?php
/**
* FileUpload.php
*
* Contains FileUpload class definition
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @package QuickDataFiles
*/

/**
* FileUpload
*
* This class subclasses Simple to provide a File Upload control.  Is owned by DataForm
* @example /var/www/corp/IntraDox/Documents.php
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @todo On Delete, delete the file, unless another record points to it.
* @package QuickData
* @subpackage QuickDataFields
*/
class FileUpload extends Simple
{
	var $UploadPath;
	var $DownloadURI = NULL;
	var $_UploadFile;
	
	// expects: $Caption, $LocalField, $UploadPath, $DownloadURI = NULL
	function FileUpload($Properties)
	{
		parent::Simple($Properties);
		
		if ( ! is_null($this->DownloadURI )) // add the last slash
		{
			if ( substr($this->DownloadURI, strlen($this->DownloadURI), 1) != "/" )
			{
				$this->DownloadURI .= "/";
			}
		}
	}

	function GetSQLReadyValue()
	{
		return "'" . trim($this->Value) . "'";
	}

	function HandleUpload()
	{
		// Accept upload
		//echo "Handle Upload in the house! {$_FILES[$this->LocalField]['name']}";
		if (strlen($_FILES[$this->LocalField]['name']))
		{
			//$uploaddir = "/var/www/corp/user/" . $User->UserName;
			if (!file_exists($this->UploadPath))
			{
				mkdir($this->UploadPath, 2775);
			}
			
			// changed to be a random hex string name
			$name = dechex(mt_rand(10000, 2000000000)) . strrchr($_FILES[$this->LocalField]['name'], ".");
			$this->_UploadFile = $this->UploadPath . "/" . $name;
			
			if (!move_uploaded_file($_FILES[$this->LocalField]['tmp_name'], $this->_UploadFile))
			{
				switch($_FILES[$this->LocalField]['error']){
				   case 0: //no error; possible file attack!
					 $out .= "There was a problem with your upload.";
					 break;
				   case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
				   case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
					 $out .= "The file you are trying to upload is too big.";
					 break;
				   case 3: //uploaded file was only partially uploaded
					 $out .= "The file you are trying upload was only partially uploaded.";
					 break;
				   case 4: //no file was uploaded
					 $out .= "You must select an image for upload.";
					 break;
				   default: //a default error, just in case!  :)
					 $out .= "There was a problem with your upload.";
					 break;
				}
				$out .= 'Here is some more debugging info:<pre>';
				print_r($_FILES);
				$out .= "<pre>\n";
				die();
			}
			else
			{
				//everything was successful
				$this->Value = $name;
			}
		} 
		else // no file is being uploaded, so set all to null
		{
			$this->_UploadFile = NULL;
			$this->Value = NULL;
		}		
	}
	
	function Update()
	{
		$this->HandleUpload();
		if(file_exists($this->_UploadFile))
		{
			$SQL = "UPDATE " . $this->Parent->Parent->DBTableName . " set $this->LocalField = " . $this->GetSQLReadyValue();
			$SQL .= " WHERE " . $this->Parent->Parent->PrimaryKeyName . " = " . $this->Parent->PrimaryKeyValue;
			//echo $SQL . "<BR>";
			$DB = $this->Parent->Parent->DB;
			$Query = $DB->CreateQuery($SQL);
			$Query->Close();
		}
		else
		{
			if ($this->_UploadFile != "" && ! is_null($this->_UploadFile))
			{
				$out .= "! {$_FILES[$this->LocalField]['error']} upload file:{$this->_UploadFile}";
			}
		}
	}
	
	function Render($DataRow, $DisplayMode = QD_MODE_VIEW)
	{
		$this->Value = $DataRow[$this->LocalField];
		$out = '';
		$out .= "\t<tr>\n\t\t<td class=\"" . $this->Parent->ViewCaptionCssClassName . "\"><label accesskey=\"E\" id=\"FileUpload\">$this->Caption</label></td>\n";
		$out .= "\t\t<td class=\"" . $this->Parent->ViewCssClassName . "\">\n";
		$out .= "\t\t<!-- {$this->LocalField} {$DataRow[$this->LocalField]} -->\n";

		if ( ! is_null($this->DownloadURI) && strlen($this->Value) && file_exists("{$this->UploadPath}/{$this->Value}"))
		{
			$out .= "<a href=\"" . $this->DownloadURI  . "/" . htmlspecialchars(addslashes($this->Value)) . "\">" . $this->Value . "</a>&nbsp;\n";
		}
		elseif ( strlen($this->Value)  && file_exists("{$this->UploadPath}/{$this->Value}") )
		{
			$out .= $this->Value . "&nbsp;";
		}

		if ($DisplayMode == QD_MODE_EDIT || $DisplayMode == QD_MODE_ADD )
		{
			$maxlength = $this->Size;
			$out .= "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"800000000\" />\n";
			$out .= "<input type=\"file\" name=\"$this->LocalField\"  maxlength=\"$maxlength\" class=\"" . $this->Parent->EditCssClassName . "\">";
		}
		$out .= "</td>\n\t</tr>\n";
		return $out;
	}
}
?>